package com.gator.desig_mode.translate;


import java.util.ArrayList;
import java.util.List;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class Client {

  public static void main(String[] args) {
    //集合
    ArrayList<Memento> mementos = new ArrayList<>();
    //备忘录管理集合
    Caretaker caretaker = new Caretaker(mementos);

    //创建被保存对象
    Originator originator = new Originator("狂战士", 100.0f, 100.0f);
    System.out.println("初始化战士属性列表：" + originator.toString());
    //调用保存到备忘录方法
    originator.saveStatus(caretaker);
    System.out.println("首次添加备忘录信息：" + originator.toString());
    System.out.println("备忘录下标为：" + originator.getIndex());
    System.out.println();

    //改变原有属性值
    originator.setAggressivity(10.0f);
    originator.setDefensivePower(50.0f);

    //添加到备忘录
    originator.saveStatus(caretaker);
    System.out.println("第一次改变的战士属性列表：" + originator.toString());
    System.out.println("备忘录下标为：" + originator.getIndex());
    System.out.println();

    //改变原有属性值
    originator.setAggressivity(17.0f);
    originator.setDefensivePower(52.0f);

    //添加到备忘录
    originator.saveStatus(caretaker);
    System.out.println("第二次改变的战士属性列表：" + originator.toString());
    System.out.println("备忘录下标为：" + originator.getIndex());
    System.out.println();
    //恢复


    int index = originator.getIndex();
    //获取需要恢复的对象对应下标
    Memento memento = caretaker.get(index);
    System.out.println("恢复上次状态，对应index:" + (index == 0 ? 0 : index - 1));
    //恢复  注意：本次实现的逻辑是恢复到上一次的状态，而不是最初始状态，可根据index灵活设置
    originator.restoreState(memento);

    System.out.println("恢复之后的战士属性列表：" + originator.toString());

    for (int i = 0; i < mementos.size(); i++) {
      System.out.println(mementos.get(i).toString());
    }

  }

}
